<?php

namespace App\Backend\Controllers;

use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Libs\SafeLibrary;

class BaseController extends Controller
{

    public function __construct()
    {
        //$SafeLibrary=new SafeLibrary('1');
    }
    /**
     * 操作成功返回
     */
    protected function success($data=[], $msg = '操作成功', $code = 200)
    {
        if(empty($data)){
            $data=[];
        }
        $alert=[
            'code'    => $code,
            'message' => $msg,
            'data'   => $data
        ];
        echo json_encode($alert);
        exit;
      
    }

    /**
     * 操作失败返回
     */
    protected function error($msg = '操作失败', $code = 0 , $data=[])
    {
        if(empty($data)){
            $data=[];
        }
        $alert=[
            'code'    => $code,
            'message' => $msg,
            'data'    => $data
        ];
        echo json_encode($alert);
        exit;
       
    }

    //校验规则
    protected function checkrule($data,$getRules, $getRulesMessage)
    {
        $validator = Validator::make($data,$getRules, $getRulesMessage);
        if($validator->fails()){
            $msg=[];
            $msg=[
                'code'=>'0',
                'msg'=>$validator->errors()->first(),
                'data'=> []
            ];
            echo json_encode($msg);
            exit;
        }
    }
    //过滤null值
    protected function filter_null($data)
    {
        foreach($data as $k=>$v){
            if(is_null($v)){
                unset($data[$k]);
            }
        }
        return $data;
    }

    //过滤没有穿过来的参数
    public function  filter_param($data,$request){
       //dd($data);
       $all=$request->all();
       $intersection = array_intersect($data, $all);

       return $intersection;
    }


}
